package com.szcinda.repository;

import com.szcinda.service.DateUtils;
import com.szcinda.service.TypeStringUtils;
import com.szcinda.service.dto.history.HistoryGhOrderImportDto;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class HistoryGhOrder extends BaseEntity {
    /**
     * 应收或者应付  IN 、OUT
     */
    private String type;
    /**
     * 订舱号
     */
    private String bookNo;
    /**
     * 所属机构
     */
    private String organizationId; //  保存时填充
    private String organizationName;
    /**
     * 客户
     */
    private String clientId; // 保存时填充
    private String clientName;
    /**
     * 供应商
     */
    private String carrierId; // 保存时填充
    /**
     * 过滤字段
     */
    private String carrierName;
    /**
     * 业务类型
     */
    private String businessType;
    /**
     * 计费方式
     */
    private String calculateType;
    /**
     * 提货日期
     */
    private LocalDate pickTime; // 日期格式 yyyy-mm-dd
    /**
     * 发车日期
     */
    private LocalDate sendTime;
    /**
     * 始发地
     */
    private String startPlace;
    /**
     * 目的地
     */
    private String endPlace;
    /**
     * 数量
     */
    private String count;
    /**
     * 方数(m³)
     */
    private String volume;
    /**
     * 毛重(KG)
     */
    private String weight;
    /**
     * 重泡比
     */
    private String pb;
    /**
     * 体积重(KG)
     */
    private String volume2;
    /**
     * 提货车型
     */
    private String pickType;
    /**
     * 提货线路
     */
    private String pickLine;
    /**
     * 派送车型
     */
    private String deliveryType;
    /**
     * 派送线路
     */
    private String deliveryLine;
    /**
     * 车型备注
     */
    private String carRemark;
    /**
     * 派送日期
     */
    private LocalDate deliveryTime;
    /**
     * 备注
     */
    private String remark;
    /**
     * 运输费(RMB)
     */
    private String transportFeeCarrier;
    private double transportFee;
    /**
     * 提货费(RMB)
     */
    private String pickFeeCarrier;
    private double pickFee;
    /**
     * 派送费(RMB)
     */
    private String deliveryFeeCarrier;
    private double deliveryFee;
    /**
     * 合计（RMB)
     */
    private double totalFee;

    // 是否锁定 0 未锁定 1 锁定
    private int lockStatus = 0;


    public void count() {
        this.totalFee = BigDecimal.valueOf(transportFee).add(BigDecimal.valueOf(pickFee)).add(BigDecimal.valueOf(deliveryFee)).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public void updateDate(HistoryGhOrderImportDto importDto) {
        if (this.pickTime == null) {
            try {
                this.pickTime = DateUtils.toLocalDate(importDto.getPickTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (StringUtils.isEmpty(this.sendTime)) {
            try {
                this.sendTime = DateUtils.toLocalDate(importDto.getSendTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (StringUtils.isEmpty(this.deliveryTime)) {
            try {
                this.deliveryTime = DateUtils.toLocalDate(importDto.getDeliveryTime().trim());
            } catch (Exception ignored) {
            }
        }
    }

    public void updateCarrierName() {
        Set<String> names = new HashSet<>();
        if (StringUtils.hasText(this.transportFeeCarrier)) {
            names.add(this.transportFeeCarrier);
        }
        if (StringUtils.hasText(this.pickFeeCarrier)) {
            names.add(this.pickFeeCarrier);
        }
        if (StringUtils.hasText(this.deliveryFeeCarrier)) {
            names.add(this.deliveryFeeCarrier);
        }
        this.carrierName = String.join(",", names);
    }

    public void updateBaseInfo(HistoryGhOrderImportDto importDto, String type) {
        if (StringUtils.isEmpty(this.organizationName)) {
            this.organizationName = importDto.getOrganizationName();
        }
        if (TypeStringUtils.historyInType.equals(type)) {
            if (StringUtils.isEmpty(this.clientName)) {
                this.clientName = importDto.getClientName();
            }
        }
        if (StringUtils.isEmpty(this.businessType)) {
            this.businessType = importDto.getBusinessType();
        }
        if (StringUtils.isEmpty(this.calculateType)) {
            this.calculateType = importDto.getCalculateType();
        }
        if (this.pickTime == null) {
            try {
                this.pickTime = DateUtils.toLocalDate(importDto.getPickTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (StringUtils.isEmpty(this.sendTime)) {
            try {
                this.sendTime = DateUtils.toLocalDate(importDto.getSendTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (StringUtils.isEmpty(this.startPlace)) {
            this.startPlace = importDto.getStartPlace();
        }
        if (StringUtils.isEmpty(this.endPlace)) {
            this.endPlace = importDto.getEndPlace();
        }
        if (StringUtils.isEmpty(this.count)) {
            this.count = importDto.getCount();
        }
        if (StringUtils.isEmpty(this.volume)) {
            this.volume = importDto.getVolume();
        }
        if (StringUtils.isEmpty(this.weight)) {
            this.weight = importDto.getWeight();
        }
        if (StringUtils.isEmpty(this.pb)) {
            this.pb = importDto.getPb();
        }
        if (StringUtils.isEmpty(this.volume2)) {
            this.volume2 = importDto.getVolume2();
        }
        if (StringUtils.isEmpty(this.pickType)) {
            this.pickType = importDto.getPickType();
        }
        if (StringUtils.isEmpty(this.pickLine)) {
            this.pickLine = importDto.getPickLine();
        }
        if (StringUtils.isEmpty(this.deliveryType)) {
            this.deliveryType = importDto.getDeliveryType();
        }
        if (StringUtils.isEmpty(this.deliveryLine)) {
            this.deliveryLine = importDto.getDeliveryLine();
        }
        if (StringUtils.isEmpty(this.carRemark)) {
            this.carRemark = importDto.getCarRemark();
        }
        if (StringUtils.isEmpty(this.deliveryTime)) {
            try {
                this.deliveryTime = DateUtils.toLocalDate(importDto.getDeliveryTime().trim());
            } catch (Exception ignored) {
            }
        }
        if (StringUtils.hasText(importDto.getRemark())) {
            if (StringUtils.hasText(this.remark)) {
                this.remark = this.remark + ";" + importDto.getRemark();
            } else {
                this.remark = importDto.getRemark();
            }
        }
        // 供应商直接覆盖
        if (StringUtils.hasText(importDto.getTransportFeeCarrier())) {
            this.transportFeeCarrier = importDto.getTransportFeeCarrier();
        }
        if (StringUtils.hasText(importDto.getPickFeeCarrier())) {
            this.pickFeeCarrier = importDto.getPickFeeCarrier();
        }
        if (StringUtils.hasText(importDto.getDeliveryFeeCarrier())) {
            this.deliveryFeeCarrier = importDto.getDeliveryFeeCarrier();
        }
        this.transportFee = this.transportFee + importDto.getTransportFee();
        this.pickFee = this.pickFee + importDto.getPickFee();
        this.deliveryFee = this.deliveryFee + importDto.getDeliveryFee();
        this.count();
    }
}
